home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
DELPHI32
/
GRIDS
/
MSTRGRID
/
MSTRGRID.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-06-17
|
23KB
|
600 lines
unit mStrGrid;
(* Version 1.0, 17.6.1996, Freeware, Albrecht Mengel, mengel@stat-econ.uni-kiel.de
This is a component descending from TStringGrid with some new sorting and searching properties & methods:
procedure InsertCols(where,howmuch:Integer);
procedure InsertRows(where,howmuch:Integer);
procedure DeleteCols(where,howmuch:Integer);
procedure DeleteRows(where,howmuch:Integer);
It does not matter if there are less rows/cols than you want to delete.
procedure AddCol(contents:String; delimiter:Char);
procedure AddRow(contents:String; delimiter:Char);
The contents are splitted into the single cells by the delimiter
function ModifyRow(which:integer; contents,delimiter:string):integer;
Results in the number of columns
function ModifyCol(which:integer; contents,delimiter:string):integer;
Results in the number of rows
The following properties control sorting and searching:
property KeyType:(soString,soStringCaseSensitive,soNumeric)
This is the kind, how the keys (and cells) are compared.
If you work with soNumeric, all non numbers get the same value 0.
As these zero values would flip in random order a (case insensitive) string sort
is performed after. So, first come the negatives, then the strings, and thereafter
the positives.
property KeyPos:Integer;
property KeyLen:Integer;
Here you may define, which substring of the cells is used to comparision.
(Default is KeyPos=1 & KeyLen=MaxInt)
property UseFixed:(hfNot,hfSmart,hfYes);
hfNot: The fixed rows/columns remain as they are (and are not used in searching)
hfSmart: When sorting complete rows, the fixed columns are part of the rows and
change their contents in the same manner as the rows are exchanged.
The top row (fixed rows) remain intact.
When sorting complete columns, the fixed rows change and the fixed columns not.
When sorting (single) row/column, hfSmart protects the fixed part of the row/column.
hfYes: All sorting includes the fixed columns and rows (as if they were set to 0)
procedure SortCompleteColumns(KeyRow:integer);
procedure SortCompleteRows(KeyCol:integer);
procedure SortRow(ThisRow:integer);
procedure SortCol(ThisCol:integer);
procedure SortAllRows;
The rows are sorted independand
procedure SortAllCols;
The columns are sorted independand
Searching can be done with the fixed substrings of cells
(set SearchSubstring=true and use KeyPos and KeyLen) or
anywhere in the cells (SearchSubstring=false)
property SearchSubstring:Boolean;
function FindFirst(Key:String; RowWise:Boolean; VAR ResultCol,ResultRow:Integer):Boolean;
Searches all cells, if UseFixed=hfYes or excludes the fixed cells, if UseFixed=hfNo.
If UseFixed=hfSmart, then the fixed cols are used if searching rowwise or
the fixed rows are used if not searching rowwise.
All finding methods result in true, when a cell was found.
If nothing found then ResultCol and ResultRow are set to -1.
function FindFirstInRow(Key:String; searchRow:Integer; VAR ResultCol,ResultRow:Integer):Boolean;
The same as FindFirst, but only one row is used for searching.
UseFixed=hfSmart is here the same as hfNo.
function FindFirstInCol(Key:String; searchCol:Integer; VAR ResultCol,ResultRow:Integer):Boolean;
function FindNext(VAR ResultCol,ResultRow:Integer):Boolean;
A sample searching program:
if FindFirst('steven',true{rowwise},c,r) then
repeat memo1.lines.add('Found in cells['+inttostr(c)+','+inttostr(r)+'] = "'+cells[c,r]+'"');
until not FindNext(c,r);
File can be read to or written from the table as ASCII file.
The second parameter is the column delimiter in every row.
procedure LoadFromFile(FileName,ColSep:string);
procedure SaveToFile(FileName,ColSep:String);
The demo program has one odd property: if found something in the fixed parts of the grid,
the fixed rows/cols are duplicated. (Because it is not allowed to set the cursor there to)
The file mStrList is required for compilation.
It contains a 'sister' of TStringList, which can sort in the above manner and is a complete copy
of TStringList, which can be found in \source\vcl\GRIDS.PAS.
But I had a problem with compiling it completely.
If you are good in programming OOP / VCL then please have a look in mStrList.pas and
help me fixing the problem. Until now all seems to work.
To install mStrGrid:
1. Copy mStrGrid.* and mStrList.* to a directory for your special delphi components.
2. Select the Component: Install menu.
3. Click the Add button.
4. Click the Browse button.
5. Locate and select the mStrGrid.pas file
6. Click the Ok buttons to close all the dialogs.
The Delphi component library DLL will be recompiled with the new mStrGrid component
added to the Additional page of the component palette.
Software development:
Programming consumes time, and programmed components save time.
If you like my components feel free to send me some acknowledgment.
I accept post cards of your town, money or cheques (2$ up to 20$).
This is a motivation for me to continue developing for you.
If you have some ideas to improve mStrList, mStrGrid or any other component
send me a message.
The component is copyright (C) 1996, by Albrecht Mengel. You may give copies to
others by copying the original, unmodified zip file. You may use this component
in your own projects free of charge as long as those projects are public domain,
freeware or shareware project.
The author of mStrList and mStrGrid (A. Mengel) makes no warranty of any kind,
expressed or implied, including without limitation any warranties of merchantability
and/or fitness for a particular purpose. In no event will the author be liable to you
for any additional damages, including any lost profits, lost savings, or other
incidental or consequential damages arising from the use of, or inability to use,
this software and its accompanying documentation, even if the author has been advised
of the possibility of such damages.
Albrecht Mengel, University of Kiel, Germany
Institute for Statistics & Economics
Olshausenstrasse 40-60,
D-24098 Kiel
Tel. +49-431-880-2424
Fax. +49-431-880-2673
Email: mengel@stat-econ.uni-kiel.de
http://www.stat-econ.uni-kiel.de/pers/mengel.htm
*)
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, mStrList;
type
HandleFixed = (hfNot,hfSmart,hfYes);
TmStrGrid = class(TStringgrid)
private
{ Private-Deklarationen }
fKeyType:TMSortType;
fKeyPos:Integer;
fKeyLen:Integer;
fUseFixed:HandleFixed;
SearchKey:String;
FirstSearchRow,FirstSearchCol,ActSearchRow,ActSearchCol,LastSearchRow,LastSearchCol:Integer;
SearchRowWise,SearchIsActive:Boolean;
fSearchSubstring:Boolean;
protected
{ Protected-Deklarationen }
public
{ Public-Deklarationen }
constructor Create(AOwner: TComponent);override;
published
{ Published-Deklarationen }
procedure InsertCols(where,howmuch:Integer);
procedure InsertRows(where,howmuch:Integer);
procedure DeleteCols(where,howmuch:Integer);
procedure DeleteRows(where,howmuch:Integer);
procedure AddRow(contents:String; delimiter:Char);
procedure AddCol(contents:String; delimiter:Char);
function ModifyRow(which:integer; contents,delimiter:string):integer;
function ModifyCol(which:integer; contents,delimiter:string):integer;
function FindFirst(Key:String; RowWise:Boolean; VAR ResultCol,ResultRow:Integer):Boolean;
function FindFirstInRow(Key:String; searchRow:Integer; VAR ResultCol,ResultRow:Integer):Boolean;
function FindFirstInCol(Key:String; searchCol:Integer; VAR ResultCol,ResultRow:Integer):Boolean;
function FindNext(VAR ResultCol,ResultRow:Integer):Boolean;
procedure SortCompleteColumns(KeyRow:integer);
procedure SortCompleteRows(KeyCol:integer);
procedure SortRow(ThisRow:integer);
procedure SortCol(ThisCol:integer);
procedure SortAllRows;
procedure SortAllCols;
property KeyType:TMSortType read fKeyType write fKeyType;
property KeyPos:Integer read fKeyPos write fKeyPos;
property KeyLen:Integer read fKeyLen write fKeyLen;
property UseFixed:HandleFixed read fUseFixed write fUseFixed;
procedure LoadFromFile(FileName,ColSep:string);
procedure SaveToFile(FileName,ColSep:String);
property SearchSubstring:Boolean read fSearchSubstring write fSearchSubstring;
end;
MFileError = class(Exception);
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Additional', [TmStrGrid]);
end;
FUNCTION rVAL(CONST von:String):Real;
{Reads a real value out of the string without an error message.
Returns 0 if no numeric value}
VAR bis,err:Integer; nach:Real;
BEGIN VAL(von,nach,bis);
IF bis>0 THEN VAL(copy(von,1,bis-1),nach,err);
rVAL:=nach
END;{rVAL}
constructor TmStrGrid.Create(AOwner: TComponent);
begin inherited Create(AOwner);
fKeyType:=soString;
fKeyPos:=1;
fKeyLen:=MaxInt;
end;
procedure TmStrGrid.SortCompleteColumns(KeyRow:integer);
{Spalten sortieren nach der Zeile KeyRow}
var i,j,k,rowanfang,colanfang:integer; index:TMStrList; puffer:TStringList;
begin case fUseFixed of
hfNot :begin rowanfang:=FixedRows; colanfang:=FixedCols end;
hfSmart:begin rowanfang:=0; colanfang:=FixedCols end;
{hfYes} else rowanfang:=0; colanfang:=0
end;
index:=TMStrList.create;
index.KeyPos:=fKeyPos;
index.KeyLen:=fKeyLen;
index.KeyType:=fKeyType;
index.ScipFirst:=colanfang;
puffer:=TStringList.create;
{Schlⁿsselzeile holen}
index.addstrings(Rows[KeyRow]);
{Sortierindizees bilden}
for i:=0 to colcount-1 do
index.objects[i]:=TObject(i);
{Liste sortieren}
index.sorted:=true;
index.sorted:=false;
{Zeilen vertauschen}
repeat {Suche einen Zyklus}
i:=0;
with index do
begin while (i<count) and (i=integer(objects[i])) do inc(i);
if i=count then break;
{Tausche Zyklus}
puffer.clear;
puffer.addstrings(cols[i]);
repeat j:=Integer(objects[i]);
objects[i]:=TObject(i);
if integer(objects[j])=j then break;
for k:=rowanfang to RowCount-1 do
cells[i,k]:=cells[j,k];
i:=j;
until false;
end;
for k:=rowanfang to RowCount-1 do
begin cells[i,k]:=puffer.strings[k];
objects[i,k]:=puffer.objects[k]
end;
until false;
index.free;
puffer.free
end;
procedure TmStrGrid.SortCompleteRows(KeyCol:integer);
{Zeilen sortieren nach der Spalte KeyCol}
var i,j,k,rowanfang,colanfang:integer; index:TMStrList; puffer:TStringList;
begin case fUseFixed of
hfNot :begin rowanfang:=FixedRows; colanfang:=FixedCols end;
hfSmart:begin rowanfang:=FixedRows; colanfang:=0 end;
{hfYes} else rowanfang:=0; colanfang:=0
end;
index:=TMStrList.create;
index.KeyPos:=fKeyPos;
index.KeyLen:=fKeyLen;
index.KeyType:=fKeyType;
index.ScipFirst:=rowanfang;
puffer:=TStringList.create;
{Schlⁿsselspalte holen}
index.addstrings(cols[KeyCol]);
{Sortierindizees bilden}
for i:=0 to rowcount-1 do
index.objects[i]:=TObject(i);
{Liste sortieren}
index.sorted:=true;
index.sorted:=false;
{Zeilen vertauschen}
repeat {Suche einen Zyklus}
i:=0;
with index do
begin while (i<count) and (i=integer(objects[i])) do inc(i);
if i=count then break;
{Tausche Zyklus}
puffer.clear;
puffer.addstrings(rows[i]);
repeat j:=Integer(objects[i]);
objects[i]:=TObject(i);
if integer(objects[j])=j then break;
for k:=colanfang to ColCount-1 do
cells[k,i]:=cells[k,j];
i:=j;
until false;
end;
for k:=colanfang to ColCount-1 do
begin cells[k,i]:=puffer.strings[k];
objects[k,i]:=puffer.objects[k]
end;
until false;
index.free;
puffer.free
end;
procedure TmStrGrid.SortRow(ThisRow:integer);
var colanfang:integer; index:TMStrList;
begin case fUseFixed of
hfNot :colanfang:=FixedCols;
hfSmart:colanfang:=FixedCols; else
{hfYes} colanfang:=0
end;
index:=TMStrList.create;
index.KeyPos:=fKeyPos;
index.KeyLen:=fKeyLen;
index.KeyType:=fKeyType;
index.ScipFirst:=colanfang;
{Go!}
index.clear;
index.addstrings(Rows[ThisRow]);
index.sorted:=true;
index.sorted:=false;
Rows[ThisRow].clear;
Rows[ThisRow].addstrings(index);
end;
procedure TmStrGrid.SortCol(ThisCol:integer);
var rowanfang:integer; index:TMStrList;
begin case fUseFixed of
hfNot :rowanfang:=FixedRows;
hfSmart:rowanfang:=FixedRows; else
{hfYes} rowanfang:=0;
end;
index:=TMStrList.create;
index.KeyPos:=fKeyPos;
index.KeyLen:=fKeyLen;
index.KeyType:=fKeyType;
index.ScipFirst:=rowanfang;
{Go!}
index.addstrings(Cols[ThisCol]);
index.sorted:=true;
index.sorted:=false;
Cols[ThisCol].clear;
Cols[ThisCol].addstrings(index);
end;
procedure TmStrGrid.SortAllRows;
{Alle Zeilen unabhΣngig voneinander sortieren}
var i,rowanfang,colanfang:integer; index:TMStrList;
begin case fUseFixed of
hfNot :begin rowanfang:=FixedRows; colanfang:=FixedCols end;
hfSmart:begin rowanfang:=0; ; colanfang:=FixedCols end;
{hfYes} else rowanfang:=0; colanfang:=0
end;
index:=TMStrList.create;
index.KeyPos:=fKeyPos;
index.KeyLen:=fKeyLen;
index.KeyType:=fKeyType;
index.ScipFirst:=colanfang;
for i:= Rowanfang to RowCount-1 do
begin index.clear;
index.addstrings(Rows[i]);
index.sorted:=true;
index.sorted:=false;
Rows[i].clear;
Rows[i].addstrings(index);
end end;
procedure TmStrGrid.SortAllCols;
{Alle Spalten unabhΣngig voneinander sortieren}
var i,rowanfang,colanfang:integer; index:TMStrList;
begin case fUseFixed of
hfNot :begin rowanfang:=FixedRows; colanfang:=FixedCols end;
hfSmart:begin rowanfang:=FixedRows; colanfang:=0 end;
{hfYes} else rowanfang:=0; colanfang:=0
end;
index:=TMStrList.create;
index.KeyPos:=fKeyPos;
index.KeyLen:=fKeyLen;
index.KeyType:=fKeyType;
index.ScipFirst:=rowanfang;
for i:= colanfang to ColCount-1 do
begin index.clear;
index.addstrings(Cols[i]);
index.sorted:=true;
index.sorted:=false;
Cols[i].clear;
Cols[i].addstrings(index);
end end;
function TmStrGrid.ModifyRow(which:integer; contents,delimiter:string):integer;
var p,c,len:integer;
begin c:=0;
len:=length(delimiter)-1;
repeat p:=pos(delimiter,contents);
if p=0 then break;
cells[c,which]:=copy(contents,1,p-1);
system.delete(contents,1,p+len);
inc(c)
until false;
cells[c,which]:=contents;
ModifyRow:=c+1
end;
function TmStrGrid.ModifyCol(which:integer; contents,delimiter:string):integer;
var p,r,len:integer;
begin r:=0;
len:=length(delimiter)-1;
repeat p:=pos(delimiter,contents);
if p=0 then break;
cells[which,r]:=copy(contents,1,p-1);
system.delete(contents,1,p+len);
inc(r)
until false;
cells[which,r]:=contents;
ModifyCol:=r+1
end;
procedure TmStrGrid.LoadFromFile(FileName,ColSep:string);
var f:textfile; zeile:string; MaxCol,z,s:integer;
begin assignfile(f,FileName);
reset(f);
if IOResult <> 0 then
raise MFileError.Create('File '+Filename+' not found');
z:=-1;
MaxCol:=0;
while not eof(f) do
begin readln(f,zeile);
if zeile='' then continue;
inc(z);
if z>FixedRows then RowCount:=z+1;
s:=ModifyRow(z,zeile,ColSep);
if s>MaxCol then MaxCol:=s;
end;
RowCount:=z+1;
ColCount:=MaxCol;
closefile(f)
end;
procedure TmStrGrid.SaveToFile(FileName,ColSep:String);
var f:textfile; z,s:integer;
begin assignfile(f,FileName);
rewrite(f);
for z:=0 to RowCount-1 do
begin for s:=0 to ColCount-1 do
begin if s>0 then write(f,ColSep);
write(f,cells[s,z]);
end;
writeln(f)
end;
closefile(f)
end;
procedure TmStrGrid.InsertCols(where,howmuch:Integer);
var i:Integer;
begin ColCount:=ColCount+howmuch;
for i:=ColCount-1 downto where+howmuch do
Cols[i]:=Cols[i-howmuch];
cols[where].clear;
for i:=where+1 to where+howmuch-1 do
Cols[i]:=Cols[where];
end;
procedure TmStrGrid.InsertRows(where,howmuch:Integer);
var i:Integer;
begin RowCount:=RowCount+howmuch;
for i:=RowCount-1 downto where+howmuch do
Rows[i]:=Rows[i-howmuch];
Rows[where].clear;
for i:=where+1 to where+howmuch-1 do
Rows[i]:=Rows[where];
end;
procedure TmStrGrid.DeleteCols(where,howmuch:Integer);
var i:Integer;
begin if ColCount-where<howmuch then howmuch:=ColCount-where;
for i:=where to ColCount-1 do
Cols[i]:=Cols[i+howmuch];
ColCount:=ColCount-howmuch;
end;
procedure TmStrGrid.DeleteRows(where,howmuch:Integer);
var i:Integer;
begin if RowCount-where<howmuch then howmuch:=RowCount-where;
for i:=where to RowCount-1 do
Rows[i]:=Rows[i+howmuch];
RowCount:=RowCount-howmuch;
end;
procedure TmStrGrid.AddRow(contents:String; delimiter:Char);
begin RowCount:=RowCount+1;
ModifyRow(RowCount-1,contents,delimiter);
end;
procedure TmStrGrid.AddCol(contents:String; delimiter:Char);
begin ColCount:=ColCount+1;
ModifyCol(ColCount-1,contents,delimiter);
end;
function TmStrGrid.FindFirst(Key:String; RowWise:Boolean; VAR ResultCol,ResultRow:Integer):Boolean;
begin SearchKey:=Key; SearchRowWise:=RowWise; SearchIsActive:=true;
case fUseFixed of
hfNot:begin FirstSearchRow:=FixedRows; FirstSearchCol:=FixedCols end;
hfSmart:if rowwise
then begin FirstSearchRow:=FixedRows; FirstSearchCol:=0; end
else begin FirstSearchRow:=0; FirstSearchCol:=FixedCols; end;
hfYes:begin FirstSearchRow:=0; FirstSearchCol:=0 end;
end;
ActSearchRow:=FirstSearchRow; LastSearchRow:=RowCount-1;
ActSearchCol:=FirstSearchCol; LastSearchCol:=ColCount-1;
FindFirst:=FindNext(ResultCol,ResultRow);
end;
function TmStrGrid.FindFirstInRow(Key:String; searchRow:Integer; VAR ResultCol,ResultRow:Integer):Boolean;
begin SearchKey:=Key; SearchRowWise:=true; SearchIsActive:=true;
FirstSearchRow:=searchRow; ActSearchRow:=searchRow; LastSearchRow:=searchRow;
if fUseFixed=hfNot
then FirstSearchCol:=FixedCols
else FirstSearchCol:=0;
ActSearchCol:=FirstSearchCol; LastSearchCol:=ColCount-1;
FindFirstInRow:=FindNext(ResultCol,ResultRow);
end;
function TmStrGrid.FindFirstInCol(Key:String; searchCol:Integer; VAR ResultCol,ResultRow:Integer):Boolean;
begin SearchKey:=Key; SearchRowWise:=false; SearchIsActive:=true;
FirstSearchCol:=searchCol; ActSearchCol:=searchCol; LastSearchCol:=searchCol;
if fUseFixed=hfNot
then FirstSearchRow:=FixedRows
else FirstSearchRow:=0;
ActSearchRow:=FirstSearchRow;
LastSearchRow:=RowCount-1;
FindFirstInCol:=FindNext(ResultCol,ResultRow);
end;
function TmStrGrid.FindNext(VAR ResultCol,ResultRow:Integer):Boolean;
var SearchReal:Real; found:boolean;
function next:boolean;
begin next:=false;
if SearchRowWise
then begin inc(ActSearchCol);
if ActSearchCol>LastSearchCol then
begin ActSearchCol:=FirstSearchCol;
inc(ActSearchRow);
if ActSearchRow>LastSearchRow then exit
end end
else begin inc(ActSearchRow);
if ActSearchRow>LastSearchRow then
begin ActSearchRow:=FirstSearchRow;
inc(ActSearchCol);
if ActSearchCol>LastSearchCol then exit
end end;
next:=true
end;
begin if not SearchIsActive then
begin FindNext:=false;
ResultRow:=-1; ResultCol:=-1;
exit
end;
SearchReal:=0;
{Suchwort konvertieren}
case fKeyType of
soNumeric:SearchReal:=Rval(SearchKey);
soString:if fSearchSubstring then SearchKey:=UpperCase(SearchKey);
end;
found:=false;
if fSearchSubstring then
repeat {Suche Passenden}
case fKeyType of
soString:if pos(SearchKey,UpperCase(cells[ActSearchCol,ActSearchRow]))>0 then
begin found:=true; break end;
soStringCaseSensitive:if pos(SearchKey,cells[ActSearchCol,ActSearchRow])>0 then
begin found:=true; break end;
soNumeric:if Rval(copy(cells[ActSearchCol,ActSearchRow],fKeyPos,fKeyLen))=SearchReal then
begin found:=true; break end;
end;
until found or not next
else
repeat {Suche Passenden}
case fKeyType of
soString:if AnsiCompareText(copy(cells[ActSearchCol,ActSearchRow],fKeyPos,fKeyLen),SearchKey)=0 then
begin found:=true; break end;
soStringCaseSensitive:if copy(cells[ActSearchCol,ActSearchRow],fKeyPos,fKeyLen)=SearchKey then
begin found:=true; break end;
soNumeric:if Rval(copy(cells[ActSearchCol,ActSearchRow],fKeyPos,fKeyLen))=SearchReal then
begin found:=true; break end;
end;
until found or not next;
if found
then begin ResultRow:=ActSearchRow; ResultCol:=ActSearchCol;
SearchIsActive:=next
end
else begin SearchIsActive:=false;
ResultRow:=-1; ResultCol:=-1
end;
FindNext:=found;
end;
end.